import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:get/get.dart';

import '../../common/styles.dart';
import '../../manager/global.dart';
import '../../widgets/noteCard.dart';
import 'logic.dart';

class SearchPage extends StatelessWidget {
  SearchPage({Key? key}) : super(key: key);

  final logic = Get.put(SearchLogic());
  final state = Get.find<SearchLogic>().state;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: logic.cancelFocus,
      child: Scaffold(
        appBar: AppBar(
            backgroundColor: Styles.backgroundColor,
            foregroundColor: Styles.mainThemeColor,
            shadowColor: Styles.shadowColor,
            leadingWidth: 40,
            leading: Container(
              alignment: Alignment.centerRight,
              child: GestureDetector(
                child: const Icon(
                  Icons.arrow_back_outlined,
                  size: 30,
                ),
                onTap: () => Get.back(),
              ),
            ),
            actions: const [
              SizedBox(
                width: 20,
              )
            ],
            title: TextFormField(
              autofocus: true,
              focusNode: state.focusNode,
              controller: state.searchController,
              textInputAction: TextInputAction.search,
              decoration: InputDecoration(
                hintText: '搜索笔记',
                filled: true,
                fillColor: Styles.lightColor,
                isCollapsed: true,
                suffixIconConstraints: const BoxConstraints(minWidth: 40),
                suffixIcon: Obx(() {
                  if (state.searchText.value.isNotEmpty) {
                    return GestureDetector(
                        onTap: logic.onCancelButton,
                        child: const Icon(
                          Icons.cancel_outlined,
                          color: Styles.mainThemeColor,
                        ));
                  }
                  return GestureDetector(
                      onTap: logic.onFocus,
                      child: const Icon(
                        Icons.search,
                        color: Styles.mainThemeColor,
                      ));
                }),
                contentPadding:
                    const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
                border: const OutlineInputBorder(
                  borderSide: BorderSide(
                    width: 1,
                    color: Styles.transparent,
                  ),
                  borderRadius: BorderRadius.all(Radius.circular(8)),
                ),
              ),
              onChanged: (value) => logic.search(),
            )),
        body: Scrollbar(
          controller: state.scrollController,
          radius: const Radius.circular(15),
          thickness: 10,
          interactive: true,
          child: Obx(() {
            int count = 0;
            Widget list = SlidableAutoCloseBehavior(
              child: ListView(
                controller: state.scrollController,
                children: [
                  ...List.generate(GlobalUtil.noteList.length, (index) {
                    if (GlobalUtil.noteList[index].title
                            .contains(state.searchText.value) ||
                        GlobalUtil.noteList[index].data
                            .contains(state.searchText.value)) {
                      count++;
                      return NoteCard(GlobalUtil.noteList[index]);
                    }
                    return const SizedBox();
                  }),
                ],
              ),
            );
            if (count == 0) {
              return const Center(child: Text('未找到有关笔记'));
            }
            return list;
          }),
        ),
      ),
    );
  }
}
